package com.kangye.exam.controller;

import com.github.pagehelper.PageInfo;
import com.kangye.exam.bean.Itembank;
import com.kangye.exam.service.ItemBankService;
import com.kangye.exam.utils.DownloadFileUtil;
import com.kangye.exam.utils.ExcelImportUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/*
 * @author 海风
 * 处理关于题目的请求,以及模板文件下载、上传
 * @data 2021-3-15 18:46:14
 * @since 1.1.5
 * */
@RestController
@RequestMapping("/itemBank")
@CrossOrigin
public class ItemBankController {

    @Resource
    private ItemBankService itemBankService;

    @Autowired
    private DownloadFileUtil downloadFileUtil;

    @Autowired
    private ExcelImportUtils excelImportUtils;

    /*
     * 按条件 模糊查询 分页
     * @itembank 题目类 从前台获取的查询条件
     * @page 当前页
     * @limit 每页条数
     * */
    @RequestMapping(value = "/limit")
    public Object find_limit(Itembank itembank, Integer page, Integer limit) {
        PageInfo pageInfo = itemBankService.find_limit(itembank, page, limit);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", pageInfo.getTotal());
        map.put("limit", pageInfo.getPageSize());
        map.put("data", pageInfo.getList());
        return map;
    }

    /*
     * 通过id查找单条题目
     * @id 题目id
     * @typeId 类型id,根据数值判断返回的url(1=单选,2=多选,3=判断,4=简答,5=编程)
     * */
    @RequestMapping("/find/{id}")
    public Object find_id(@PathVariable("id") Integer id, Integer typeId) {
        Map<String, Object> map = new HashMap<>();
        Itembank itembank = itemBankService.selectByPrimaryKey(id);
        map.put("code", 0);
        map.put("data", itembank);
        return map;
    }

    /*
     * 插入一条数据
     * @itembank 题目类,判空
     * */
    @RequestMapping("/insert")
    public Object insert(Itembank itembank) {
        return itemBankService.insert(itembank) > 0 ? "{\"code\":0}" : "{\"code\":-1}";
    }

    /*
     * 修改一条数据
     * @itembank 题目类,判空
     * */
    @RequestMapping("/update")
    public Object update(Itembank itembank) {

        return itemBankService.update(itembank) > 0 ? "{\"code\":0}" : "{\"code\":-1}";
    }

    /*
     * 删除一条数据
     * @id 需要删除的数据id
     * */
    @RequestMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Integer id) {
        return itemBankService.delete(id) > 0 ? "{\"code\":0}" : "{\"code\":-1}";
    }

    /*
     * 下载模板
     * */
    @RequestMapping("/down/excel")
    public void downloadexcel(HttpServletResponse resp) {

        ClassPathResource cpr = new ClassPathResource("/itemBank.xlsx");
        String fileName = "题目模板.xlsx";
        downloadFileUtil.download(cpr, fileName, resp);

    }

    /*
     *  上传模板
     *  @file
     * */
    @RequestMapping("/upload")
    public Object upload(MultipartFile file) {
        //判断文件是否为空
        if (file == null) {

            return "文件不能为空";
        }

        //获取文件名
        String fileName = file.getOriginalFilename();

        //验证文件名是否合格
        if (!excelImportUtils.validateExcel(fileName)) {

            return "文件类型错误!请重新下载模板";
        }

        //进一步判断文件内容是否为空（即判断其大小是否为0或其名称是否为null）
        long size = file.getSize();
        if (fileName.isEmpty() || size == 0) {

            return "文件不能为空";
        }

        //批量导入
        String message = itemBankService.batchImport(fileName, file);
         Map<String,Object> map =new HashMap<>();
         map.put("msg",message);
        return map;
    }


}
